/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.actions;

import com.ejb.UsuarioFachadaRemote;
import com.entity.Usuario;
import com.ejb.VotosFachadaRemote;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.*;

/**
 *
 * @author dario
 */
public class ListadoVotosNickAction extends org.apache.struts.action.Action {
    
    /* forward name="success" path="" */
    private static final String listadoUsuariosNick = "ExitoListVotosNick";
        
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        VotosFachadaRemote votos = lookupVotosFachadaBean();
        UsuarioFachadaRemote usuarios = lookupUsuarioFachadaBean();

        ListadoVotosNickForm aForm = (ListadoVotosNickForm)form;

        //Obtengo nick
        String nick = aForm.getNick();
        Usuario user = new Usuario();
        user = usuarios.usuarioID(nick);


        if(user == null){
            ActionMessages errores = new ActionMessages();

            errores.add("errorNick", new ActionMessage("listadoVotosNick.noNick"));

            saveErrors(request, errores);
            return mapping.getInputForward();
        }
        else{
            Long id = user.getId();

            request.getSession().setAttribute("ID", id);
            return mapping.findForward(listadoUsuariosNick);
        }
    }

    private UsuarioFachadaRemote lookupUsuarioFachadaBean() {
        try {
            Context c = new InitialContext();
            return (UsuarioFachadaRemote) c.lookup("java:comp/env/UsuarioFachadaBean");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }

    private VotosFachadaRemote lookupVotosFachadaBean() {
        try {
            Context c = new InitialContext();
            return (VotosFachadaRemote) c.lookup("java:comp/env/VotosFachadaBean");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }


}
